"""
urls.py

URL dispatch route mappings and error handlers

"""

from flask import render_template

from application import app
from application import views
from application.views import cron, importer, data_browser, ajax

## URL dispatch rules
# App Engine warm up handler
# See http://code.google.com/appengine/docs/python/config/appconfig.html#Warming_Requests
app.add_url_rule('/_ah/warmup', 'warmup', view_func=views.warmup)

# Home page
app.add_url_rule('/', 'home', view_func=views.home)

# Cron entry points
app.add_url_rule('/cron/TestApi', 'cron_TestApi', view_func=cron.TestApi)
app.add_url_rule('/cron/AllianceList', 'cron_AllianceList', view_func=cron.AllianceList)
app.add_url_rule('/cron/Sovereignty', 'cron_Sovereignty', view_func=cron.Sovereignty)

# Importer
app.add_url_rule('/importer', 'importer', view_func=importer.importer, methods=['GET', 'POST'])

# Data Browser
app.add_url_rule('/data', 'data_browser_home', view_func=data_browser.home)
app.add_url_rule('/data/towers', 'data_browser_towers', view_func=data_browser.towers)
app.add_url_rule('/data/map', 'data_browser_map', view_func=data_browser.map)

# AJAX Calls
app.add_url_rule('/ajax/map/region', 'ajax_map_region', view_func=ajax.mapRegion)
app.add_url_rule('/ajax/map/constellation', 'ajax_map_constellation', view_func=ajax.mapConstellation)
app.add_url_rule('/ajax/map/solarSystem', 'ajax_map_solarSystem', view_func=ajax.mapSolarSystem)
app.add_url_rule('/ajax/map/moon', 'ajax_map_moon', view_func=ajax.mapMoon)

## Error handlers
# Handle 404 errors
@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

# Handle 500 errors
@app.errorhandler(500)
def server_error(e):
    return render_template('500.html'), 500

